<template>
  <el-dialog
    v-model="dialogVisible"
    title="新增点检计划"
    width="900px"
    @close="closeDialog"
  >
    <el-form class="aceauForm" label-width="120px" :model="state.form">
      <el-row :gutter="12">
        <el-col :span="12">
          <el-form-item label="计划编码" required>
            <el-input v-model="state.form.planCode" placeholder="" />
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item label="计划名称">
            <el-input v-model="state.form.planName" placeholder="" />
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item label="计划周期">
            <el-select
              v-model="state.form.generateMode"
              placeholder="周期"
              style="width: 100%"
              value-key="id"
            >
              <el-option
                v-for="generateMode in generateModeRef"
                :key="generateMode.id"
                :label="generateMode.name"
                :value="generateMode.id"
              />
            </el-select>
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item label="计划频率">
            <el-input v-model="state.form.frequency" placeholder="" />
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item label="生成时间" required>
            <el-date-picker
              v-model="state.form.generateTime"
              placeholder=""
              type="datetime"
              value-format="YYYY-MM-DD HH:mm:ss"
            />
          </el-form-item>
        </el-col>
      </el-row>
    </el-form>

    <template #footer>
      <span class="dialog-footer">
        <el-button @click="dialogVisible = false">取消</el-button>
        <el-button type="primary" @click="save">保存</el-button>
      </span>
    </template>
  </el-dialog>
</template>

<script setup>
  import { localStorageConfig } from '@/config/localstorage.config'
  import { addDianjianPlanApi } from '@/api/equip/dianjian_api'

  const $baseLoading = inject('$baseLoading')
  const $baseMessage = inject('$baseMessage')

  const pkCorp = localStorage.getItem(localStorageConfig.__pkCorp)
  const dialogVisible = ref(false)

  const generateModeRef = ref([
    { id: 1, name: '天' },
    { id: 2, name: '周' },
    { id: 3, name: '月' },
  ])

  const state = reactive({
    form: {
      pkCorp: pkCorp,
    },
  })

  const openDialog = () => {
    dialogVisible.value = true
  }

  const save = async () => {
    const load = $baseLoading()
    try {
      const { code, msg } = await addDianjianPlanApi(state.form)
      if (code === 2000) {
        $baseMessage(msg, 'success')
        dialogVisible.value = false
        _emit('success-callback')
      } else {
        $baseMessage(msg, 'error')
      }
    } finally {
      load.close()
    }
  }

  defineExpose({ openDialog })
  const _emit = defineEmits(['success-callback'])
</script>
